package com.hb.easyexcel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.hb.easyexcel.entity.ExcelStudentData;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ExcelStudentDataListener extends AnalysisEventListener<ExcelStudentData> {

    private static final int BATCH_COUNT = 3;
    List<ExcelStudentData> list = new ArrayList<>();

    /**
     * 遍历每一行的记录
     *
     * @param excelStudentData
     * @param analysisContext
     */
    @Override
    public void invoke(ExcelStudentData excelStudentData, AnalysisContext analysisContext) {
        log.info("解析到的每一行:{}", excelStudentData);
        list.add(excelStudentData);
        if (list.size() >= BATCH_COUNT) {
            //TODO 存储数据到库表里
            log.info("存库中" + list.size());
            list.clear();
        }
    }

    /**
     * 遍历每一行后的收尾工作
     *
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("存剩余数据" + list.size());
        list.clear();
        log.info("全部数据解析完成");
    }
}
